package com.simen.hellobytes.code;

import com.simen.hellobytes.IOUtil;
import com.simen.hellobytes.SystemUtil;

import java.io.*;

/**
 * author: SimenHi
 * create time: 2020-08-20 11:06
 * description:
 */
public class FilterLogLine {

    public static void main(String[] args) {
        FilterLogLine filterLogLine = new FilterLogLine();

        try {
            filterLogLine/**/./**/clear/**/("boo.java", "AppLog", "d");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final int FILTER_STEP_NONE=0;
    private static final int FILTER_STEP_CLSNAME=1;
    private static final int FILTER_STEP_POINTER=2;// .
    private static final int FILTER_STEP_LEFT_PARENTHESES=3;// (
    private static final int FILTER_STEP_COMMA=4;// ,
    private static final int FILTER_STEP_RIGHT_PARENTHESES=5;// )
    private static final int FILTER_STEP_SEMICOLON=6;// ;

    private void clear(String fileName, String clsName, String... methodName) throws IOException {
        String body = IOUtil.readStreamContent(new FileInputStream(fileName));

        StringReader stringReader = new StringReader(body);
        BufferedReader bufferedReader = new BufferedReader(stringReader);
        FilterCommentLine filterCommentLine = new FilterCommentLine();

        String line = null;
        int clsNameCharLenth = clsName.length();
        StringBuilder outfileBuf = new StringBuilder();
        int filterStep=FILTER_STEP_NONE;
        boolean foundDoubleQuote=false;
        while ((line = bufferedReader.readLine()) != null) {
            String noCommentLine = filterCommentLine.swallowLine(line.trim());
            if (noCommentLine != null) {
                if(filterStep==FILTER_STEP_NONE) {
                    int index = noCommentLine.indexOf(clsName);
                    if (index != -1) {
                        while (true) {
                            if (checkVariableNamePattern(noCommentLine, clsName, clsNameCharLenth, index)) {
                                filterStep=FILTER_STEP_CLSNAME;

                                index=index+clsNameCharLenth;
                                if(index>=noCommentLine.length()){
                                    outfileBuf.append(SystemUtil.getLineSeparator());
                                    break;
                                }else{
                                    for (int i = index; i < noCommentLine.length(); i++) {
                                        char c=noCommentLine.charAt(i);
                                        if(foundDoubleQuote){
                                            //双引号只会成对出现在同一行代码中，不会跨行
                                            if(c=='"'){
                                                if(!GrammarUtil.beforeQuoteContainsSingleBackSlash(noCommentLine,i-1)){
                                                    foundDoubleQuote=false;
                                                }
                                            }
                                        }else {
                                            if (c == '.') {
                                                if (filterStep != FILTER_STEP_CLSNAME) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                filterStep = FILTER_STEP_POINTER;
                                            } else if (c == '"') {
                                                if (filterStep != FILTER_STEP_LEFT_PARENTHESES) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                foundDoubleQuote = true;
                                            } else if (c == '(') {
                                                if (filterStep != FILTER_STEP_POINTER) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                filterStep = FILTER_STEP_LEFT_PARENTHESES;
                                            } else if (c == ',') {
                                                if (filterStep != FILTER_STEP_POINTER) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                filterStep = FILTER_STEP_COMMA;
                                            } else if (c == ')') {
                                                if (filterStep != FILTER_STEP_POINTER) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                filterStep = FILTER_STEP_LEFT_PARENTHESES;
                                            } else if (c == ';') {
                                                if (filterStep != FILTER_STEP_POINTER) {
                                                    throw new IOException("语法错误： " + noCommentLine);
                                                }
                                                filterStep = FILTER_STEP_LEFT_PARENTHESES;
                                            } else {

                                            }
                                        }
                                    }
                                }
                            } else {
                                index = noCommentLine.indexOf(clsName, index + 1);
                                if (index == -1) {

                                    break;
                                }
                            }
                        }
                    } else {
                        outfileBuf.append(noCommentLine);
                    }
                }else if(filterStep==FILTER_STEP_CLSNAME){
                }else if(filterStep==FILTER_STEP_POINTER){
                }else if(filterStep==FILTER_STEP_LEFT_PARENTHESES){
                }else if(filterStep==FILTER_STEP_RIGHT_PARENTHESES){
                }else if(filterStep==FILTER_STEP_SEMICOLON){
                }
            } else {
                outfileBuf.append(SystemUtil.getLineSeparator());
            }

            if(foundDoubleQuote){
                throw new IOException("语法错误： 未关闭的双引号->"+line);
            }
        }
        filterCommentLine.swallowEnd();
    }

    /**
     * @param line
     * @param clsName
     * @param clsNameCharLenth
     * @param index            index大于等于0
     * @return
     */
    private boolean checkVariableNamePattern(String line, String clsName, int clsNameCharLenth, int index) {
        if (index != 0) {
            //判断变量名前面的字符类型
            char lastC = line.charAt(index - 1);
            if (lastC != ' ') {
                return false;
            }
        }

        //判断变量名后面的字符类型
        int index2 = index + clsNameCharLenth;
        if (index2 == line.length()) {
            return true;
        } else {
            char c2 = line.charAt(index2);
            return c2 == ' ' || c2 == '.';
        }
    }

    private boolean isValidVariableNameChar(String line, int charIndex, boolean containsUnderline) {
        if (charIndex < 0) return false;
        if (charIndex >= line.length()) return false;

        char c = line.charAt(charIndex);
        if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (containsUnderline && c == '_')) {
            return true;
        }

        return false;
    }

}
